{% extends "base.html" %}
{% block content %}
<h2>Medical Records</h2>

<section class="card">
  <h3>Create Medical Record</h3>
  <form method="post" action="{{ url_for('create_medical_record') }}" class="form-grid">
    <input name="appointment_id" placeholder="Appointment ID" required />
    <input name="patient_id" placeholder="Patient ID" required />
    <input name="diagnosis" placeholder="Diagnosis" required />
    <textarea name="symptoms" placeholder="Symptoms"></textarea>
    <textarea name="observations" placeholder="Observations"></textarea>
    <button type="submit">Create</button>
  </form>
</section>

<section class="card">
  <h3>Patient History</h3>
  <form method="get">
    <input name="patient_id" placeholder="Patient ID to view history" />
    <button type="submit">Load</button>
  </form>

  {% if records %}
  <table class="striped">
    <thead><tr><th>ID</th><th>Appointment</th><th>Diagnosis</th><th>Symptoms</th><th>Observations</th><th>Actions</th></tr></thead>
    <tbody>
      {% for r in records %}
      <tr>
        <td>{{ r.record_id }}</td>
        <td>{{ r.appointment_id }}</td>
        <td>{{ r.diagnosis }}</td>
        <td>{{ r.symptoms }}</td>
        <td>{{ r.observations }}</td>
        <td>
          <form method="post" action="{{ url_for('delete_medical_record', record_id=r.record_id) }}" style="display:inline">
            <button class="btn danger" type="submit" onclick="return confirm('Delete record #{{ r.record_id }}?')">Delete</button>
          </form>
        </td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
  {% else %}
    <p class="muted">No records loaded. Use the form above to view a patient's history.</p>
  {% endif %}
</section>
{% endblock %}
